package com.whfc.ms.api.md.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.md.service.MdSafetyBoardService;
import com.whfc.safety.dto.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 数据大屏-安全
 *
 * @author qzexing
 * @version 1.0
 * @date 2021/11/17 11:50
 */
@RestController
@RequestMapping("/ms/api/md/board/safety")
public class MdSafetyBoardController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MdSafetyBoardService mdSafetyBoardService;


    @GetMapping("/check/type/statistics")
    public Result checkTypeStatistics(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                      @RequestParam("deptId") Integer deptId,
                                      @RequestParam(value = "startTime", required = false)
                                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                      @RequestParam(value = "endTime", required = false)
                                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        logger.info("大屏-安全-问题类型统计, loginer:{}, deptId:{}, startTime:{}, endTime:{}",
                user.getUsername(), deptId, startTime, endTime);
        ListData<MdSafetyStatisticsDTO> listData = mdSafetyBoardService.getCheckTypeStatistics(deptId, startTime, endTime);
        return ResultUtil.success(listData);
    }

    @GetMapping(value = "/check/type/statistics", params = {"pageNum", "pageSize"})
    public Result checkTypeStatistics(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                      @RequestParam("deptId") Integer deptId,
                                      @RequestParam(value = "startTime", required = false)
                                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                      @RequestParam(value = "endTime", required = false)
                                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                      @RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize) {
        logger.info("大屏-安全-问题类型统计（分页）, loginer:{}, deptId:{}, startTime:{}, endTime:{}, pageNum:{}, pageSize:{}",
                user.getUsername(), deptId, startTime, endTime, pageNum, pageSize);
        PageData<MdSafetyStatisticsDTO> pageData = mdSafetyBoardService
                .getCheckTypeStatistics(deptId, startTime, endTime, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/check/part/statistics")
    public Result checkPartStatistics(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                      @RequestParam("deptId") Integer deptId,
                                      @RequestParam(value = "startTime")
                                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                      @RequestParam(value = "endTime")
                                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        logger.info("大屏-安全-问题类型统计, loginer:{}, deptId:{}, startTime:{}, endTime:{}",
                user.getUsername(), deptId, startTime, endTime);
        ListData<SafetyCheckPartStatisticalDTO> listData = mdSafetyBoardService.getCheckPartStatistics(deptId, startTime, endTime);
        return ResultUtil.success(listData);
    }

    @GetMapping("/check/list")
    public Result checkList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam("deptId") Integer deptId,
                            @RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer pageSize) {
        logger.info("大屏-安全-检查记录列表, loginer:{}, deptId:{}, pageNum:{}, pageSize:{}",
                user.getUsername(), deptId, pageNum, pageSize);
        PageData<MdSafetyCheckDTO> pageData = mdSafetyBoardService.getCheckList(deptId, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/check/analysis")
    public Result checkAnalysis(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("deptId") Integer deptId) {
        logger.info("大屏-安全-问题分析, loginer:{}, deptId:{}", user.getUsername(), deptId);
        MdSafetyCheckAnalysisDTO analysisDTO = mdSafetyBoardService.getCheckAnalysis(deptId);
        return ResultUtil.success(analysisDTO);
    }

    @GetMapping("/check/log")
    public Result checkLog(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestParam("deptId") Integer deptId,
                           @RequestParam("pageNum") Integer pageNum,
                           @RequestParam("pageSize") Integer pageSize) {
        logger.info("大屏-安全-问题动态, loginer:{}, deptId:{}, pageNum:{}, pageSize:{}",
                user.getUsername(), deptId, pageNum, pageSize);
        PageData<MdSafetyCheckLogDTO> pageData = mdSafetyBoardService.getCheckLog(deptId, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/check/unfinished")
    public Result checkUnfinished(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("deptId") Integer deptId,
                                  @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                                  @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        logger.info("大屏-安全-未销项,loginer:{},deptId:{},startTime:{},endTime:{}", user.getUsername(), deptId, startTime, endTime);
        SafetyCheckUnfinishedDTO data = mdSafetyBoardService.qualityCheckUnfinished(deptId, startTime, endTime);
        return ResultUtil.success(data);
    }


}
